<template>
    <div class="time">
        当前时间： {{ time }}
    </div>
</template>
<script setup>
import dayjs from "dayjs";  //时间转换插件
import { ref } from 'vue'
import { onMounted, onBeforeUnmount } from "vue";

let timer = 0;
let time = ref("");

onMounted(() => {
    timer = setInterval(() => {
        //设置定时器
        getTime(); //自定义事件
    }, 1000);
});

onBeforeUnmount(() => {
    clearInterval(timer); //清除定时器
    timer = 0;
});

const getTime = () => {
    time.value = dayjs().format("YYYY年MM月DD日-HH时mm分ss秒");
}

</script>
<style lang="scss" scoped></style>